package com.doit.day01;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class _02_作业 {
    public static void main(String[] args) throws Exception {
        //获取zk对象
        ZooKeeper zk = ZkCliDemo.getCli();
        //需求：在zk里面创建一个节点，目录是/teachers  xiaotao   -s  -e
//        String s = zk.create("/teachers", "xiaotao".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //将刚才创建的/teachers 对应value的值 xiaotao   改成xiaojiang
//        Stat stat = zk.setData("/teachers", "xiaojiang".getBytes(StandardCharsets.UTF_8), -1);
        //将刚才创建的/teachers 节点删除掉
//        zk.delete("/doit",-1);

        rmr(zk,"/doit");


    }


    public static void rmr(ZooKeeper zk,String path) throws Exception {
        //首先看一下这个节点下面的子节点是什么情况
        List<String> children = zk.getChildren(path, null);
        for (String child : children) {
            //child这个里面有没有呢，他是不是一个空节点呢？
            if(child != null || child.length() != 0){
                //如果不是一个空节点，那么我就要就行往下挖
                rmr(zk,path+"/"+child);
            }else {
                //是一个空节点
                zk.delete(path+"/"+child,-1);
                System.out.println("正在删除的节点是："+path+"/"+child);
            }
        }
        zk.delete(path,-1);
        System.out.println("正在删除的节点是："+path);
    }

}
